<?php
function itx_all_sidebar(){
    $bar=array(
        array('thename'=>'Left Wide','description'=>'Wide sidebar on the left side.' ,
			'name'=>'sidebar','pos'=>'left','size'=>'wide'),
        array('thename'=>'Left Narrow #1','description'=>'Narrow sidebar on the leftmost side.',
			'name'=>'sidebar','pos'=>'left','size'=>'narrow','num'=>1),
        array('thename'=>'Left Narrow #2','description'=>'Narrow sidebar on the left side, right of the leftmost narrow sidebar.',
			'name'=>'sidebar','pos'=>'left','size'=>'narrow','num'=>2),
        array('thename'=>'Inner Top','description'=>'Widget area that only appear in front page at the top of the content.',
			'name'=>'innertop','title_class'=>'','body_class'=>''),
        array('thename'=>'Inner Bottom','description'=>'Widget area that only appear in front page at the bottom of the content.',
			'name'=>'innerbottom','title_class'=>'','body_class'=>''),
        array('thename'=>'Right Wide','description'=>'Wide sidebar on the right side.',
			'name'=>'sidebar','pos'=>'right','size'=>'wide'),
        array('thename'=>'Right Narrow #1','description'=>'Narrow sidebar on the right side, left of the rightmost narrow sidebar.',
			'name'=>'sidebar','pos'=>'right','size'=>'narrow','num'=>1),
        array('thename'=>'Right Narrow #2','description'=>'Narrow sidebar on the rightmost side.',
			'name'=>'sidebar','pos'=>'right','size'=>'narrow','num'=>2),
        array('thename'=>'Footer','description'=>'Widget Area in Footer Section',
			'name'=>'footer','title_class'=>'','body_class'=>''),
        array('thename'=>'Left','description'=>'Sidebar on the left side.',
			'name'=>'sidebar','pos'=>'left','size'=>'narrow'),
        array('thename'=>'Right','description'=>'Sidebar on the right side.',
			'name'=>'sidebar','pos'=>'right','size'=>'narrow'),
    );

    switch (itx_get_option('layout','column')){
        case 1: $ars=array(8,3,4);break;
        case 21: $ars=array(5,6,7,3,4,8);break;
        case 22: $ars=array(6,7,5,3,4,8);break;
        case 23: $ars=array(0,1,2,3,4,8);break;
        case 24: $ars=array(1,2,0,3,4,8);break;
        case 3: $ars=array(9,10,3,4,8);break;
    }

    $default=array('title_class'=>'ui-widget-header ui-corner-all','body_class'=>'ui-widget ui-widget-content ui-corner-all');
    foreach ($ars as $v){
        $bars[]=wp_parse_args($bar[$v],$default);
    }
    return $bars;
}

function itx_get_sidebar($args){
    $bars=itx_all_sidebar();
	$pos=$names=$poss='';
    extract($args);
    if ($name){
        foreach ($bars as $k=>$named){
            if ($named['name']==$name){$names[$k]=$named;}
        }
        $bars=$names;
        if($pos&&$names){
            foreach ($names as $k=>$posed){
                if ($posed['pos']==$pos){$poss[$k]=$posed;}
            }
        $bars=$poss;
        }
    }
    return $bars;
}

if ( function_exists('register_sidebar') ){itx_reg_sidebar();}

function itx_reg_sidebar(){
    foreach (itx_all_sidebar() as $k=>$args){
		$pos='';
        extract($args);
        register_sidebar(array(
            'id'=> 'sidebar-'.($k+1),
            'name'=> "$thename Widget Area",
			'description'=> $description,
            'before_widget' => '<li id="%1$s" class="widget '.$body_class.' '.$pos.' %2$s"><div class="widgetwrap">',
            'after_widget' => '</div></li>',
            'before_title' => '<h3 class="'.$title_class.'">',
            'after_title' => '</h3>',
        ));
    }
}

function itx_sidebar($args){
    $args=wp_parse_args($args);
    $bars=itx_get_sidebar($args);
    if ($bars){
		$pos=$size='';
		extract($args);
		if ($pos)$pos='-'.$pos;
		echo '<div id="'.$name.$pos.'" class="'.$name.'">';
        foreach ( $bars as $k=>$v){
            extract($v);
            $size_class=($size)?" $name-$size":'';
            echo"<div class='$name-$pos$size_class'>";
                if (itx_active_sidebar($k+1)) {
                    echo '<ul>';
                    dynamic_sidebar($k+1);
                    echo '</ul>';
                } else {itx_default_sidebar($size,$args);}
            echo '<div class="clear"></div></div>';
        }
        echo '</div>';
    };
}

function itx_default_sidebar($thesize,$args){
	if ($thesize!='wide') return;
    $bars=itx_get_sidebar($args);
	foreach ( $bars as $k=>$v){
		extract($v);
		if ($size!='wide') return;
		if ($size) {$size_class=" $name-$size";}
		echo"<div class='$name-$pos$size_class'>";
			if (function_exists('the_widget')){
                the_widget('itx_tabbed_sidebar');
            }
			echo'<ul>
            <li class="widget '.$body_class.'"><div class="widgetwrap">';
                get_search_form();
            echo '</div></li>';
            echo '<li class="widget '.$body_class.'"><div class="widgetwrap">
                <div id="calendar_wrap">';
                get_calendar();
                echo '</div>
            </div></li>
            <li class="widget '.$body_class.'"><div class="widgetwrap">
                <h3 class="'.$title_class.'">Pages</h3><ul>';
                wp_list_pages('title_li=');
            echo '</ul></div></li>
			<li class="widget '.$body_class.'"><div class="widgetwrap">
                <h3 class="'.$title_class.'">Tags</h3>';
                wp_tag_cloud();
            echo '</div></li>
            <li class="widget '.$body_class.'"><div class="widgetwrap">
                <h3 class="'.$title_class.'">Meta</h3><ul>';
                wp_register();?>
                <li><?php wp_loginout(); ?></li>
                <li><a href="<?php bloginfo('rss2_url'); ?>" title="<?php echo esc_attr(__('Syndicate this site using RSS 2.0')); ?>"><?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
                <li><a href="<?php bloginfo('comments_rss2_url'); ?>" title="<?php echo esc_attr(__('The latest comments to all posts in RSS')); ?>"><?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
                <li><a href="http://wordpress.org/" title="<?php echo esc_attr(__('Powered by WordPress, state-of-the-art semantic personal publishing platform.')); ?>">WordPress.org</a></li>
                <?php wp_meta();
            echo '</ul></div></li>
		</ul></div>';
	}
}

function itx_active_sidebar( $index ) {
	if (function_exists('is_active_sidebar')) return is_active_sidebar($index);
	$index = ( is_int($index) ) ? "sidebar-$index" : sanitize_title($index);
	$sidebars_widgets = wp_get_sidebars_widgets();
	if ( !empty($sidebars_widgets[$index]) )
		return true;
	return false;
}

function itx_active_widget($id_base = false) {
    global $wp_registered_widgets;

    //excluding innertop & innerbottom;
    foreach (itx_all_sidebar() as $k=>$args){
	if ($args['name']=='innertop'||$args['name']=='innerbottom') $ex[]=$k+1;
    }
    $sidebars_widgets = wp_get_sidebars_widgets();
    if ( is_array($sidebars_widgets) ) {
	foreach ( $sidebars_widgets as $sidebar => $widgets ) {
	    if ('wp_inactive_widgets'==$sidebar||"sidebar-$ex[0]" ==$sidebar || "sidebar-$ex[1]" ==$sidebar)
		continue;
	    if (is_array($widgets)) {
		foreach ( $widgets as $widget ) {
		    if ( $id_base && _get_widget_id_base($widget) == $id_base ) return $sidebar;
		}
	    }
	}
    }
    return false;
}

function itx_layout_styles(){
    extract(itx_get_option('layout'));
    $sidebear=(is_numeric($sidebar)) ? $sidebar:300;
    if($wrapping=='fixed'){
        if(!is_numeric($wrap)) $wrap=300;
        $wrapper=$wrap.'px';
        $wmargin='auto';
    } else {
        $wrapper='100%';
        $wmargin='0';
    }

	$cfloat='left';
    if ($sidebarunit=='px') {
        $wide=$sidebar-12;
        $narrow=$wide/2;
        if ($wrapping=='fixed'){
            $content=$wrap-$sidebar.'px';
	    $main='#mainwrap{margin:0}';
	    if ($column==23||$column==24){$cfloat="right";}
	    elseif ($column==3){
		$nside=$sidebar/2;
		$cpos=$wrap-$nside;
		$main="#mainwrap{margin:0;right:".($cpos-1)."px;position:relative;}#content{left:100%}#sidebar-right{left:{$cpos}px;}#sidebar-left{left:{$nside}px}";
	    }
        } elseif ($column!=1){
            if ($column==21||$column==22){
                $mainmargin=-$sidebar;
                $main="#sidebar-right{left:{$sidebar}px}";
            } elseif ($column==23||$column==24){
                $mainmargin=0;
		$main="#sidebar-left{left:15px}";
            } else{
                $mainmargin=-$narrow-10;
                $main="#sidebar-right{left:{$narrow}px}#sidebar-left{left:".($narrow+20)."px}\n";
            }
            $main.="#mainwrap{width:200%;right:100%;float:left;position:relative;margin-left:{$mainmargin}px;}\n";
            $main.="#contentpad{margin-left:".($sidebar+20)."px;margin-right:10px;}\n";
            $content="50%;left:50%;";
        }
    }
    else {
        $content=100-$sidebar.'%';
        $wide='100';
        $narrow='50';
	if ($column==3){
	    $content=98-$sidebar.'%';
	    $nside=$sidebar/2+1;
	    $cpos=100-$nside;
	    $main="#mainwrap{margin:0;right:".($cpos-1)."%;position:relative;}#content{left:99%}#sidebar-right{left:".($cpos-2)."%;}#sidebar-left{left:$nside%}";
	}elseif ($column==23||$column==24){
	    $cfloat="right";
	}
    }
    if ($column==1){$content='100%';}
    elseif ($column==3){
        if ($sidebarunit=='px') {$sidebar=$narrow;}
        else {$sidebar=$sidebar/2;$narrow='100';}
    }
    $itop=100/$innertop;
    $ibottom=100/$innerbottom;
    $footer=100/$footer;
    echo "
.wrap{width:$wrapper;margin:$wmargin;}
$main#content{margin-top:15px;width:$content;float:$cfloat;position:relative;overflow:hidden;}
#sidebar-left,#sidebar-right{width:$sidebar$sidebarunit;position:relative;}
.sidebar-narrow{width:$narrow$sidebarunit;}
.sidebar-wide{width: $wide$sidebarunit;}
#innertop .widget{width:$itop%;}
#innerbottom .widget{width:$ibottom%;}
#footer .widget{width:$footer%;}
";
}
?>